package com.kyle.silverbullet.utils;

import java.time.LocalDateTime;

public class Result<T> {

    private ResultEnum resultEnum;

    private T data;//数据

    //以下方法快速生成对象
    public static <S> Result<S> isOk() {
        Result<S> result = new Result<>();
        result.setResultEnum(ResultEnum.SUCCESS);
        result.setData(null);
        return result;
    }

    public static <S> Result<S> isOk(S data) {
        Result<S> result = new Result<>();
        result.setResultEnum(ResultEnum.SUCCESS);
        result.setData(data);
        return result;
    }

    public static <S> Result<S> isFail() {
        Result<S> result = new Result<>();
        result.setResultEnum(ResultEnum.UNKNOWN_ERROR);
        result.setData(null);
        return result;
    }

    public static <S> Result<S> isFail(ResultEnum resultEnum) {
        Result<S> result = new Result<>();
        result.setResultEnum(resultEnum);
        return result;
    }

    public static <S> Result<S> isFail(ResultEnum resultEnum, S data) {
        Result<S> result = new Result<>();
        result.setResultEnum(resultEnum);
        result.setData(data);
        return result;
    }

    public int getCode() {
        return resultEnum.getCode();
    }

    public String getMsg() {
        return resultEnum.getMsg();
    }

    public void setResultEnum(ResultEnum resultEnum) {
        this.resultEnum = resultEnum;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public LocalDateTime getLocalDateTime() {
        return LocalDateTime.now();
    }

    @Override
    public String toString() {
        return "Result{" +
                "resultEnum=" + resultEnum +
                ", data=" + data +
                '}';
    }
}